<template>
	<BaseContainer
		class="report-table-wrapper"
		v-loading="loading"
		element-loading-text="加载中..."
		element-loading-background="rgba(255, 255, 255, 0.8)"
	>
		<div class="t-header">
			<div class="article-title">
				<span>南京市养老机构改护理型床位补贴申请表</span>
			</div>
		</div>
		<div class="flex align-center gap-20 space-between">
			<div style="min-width: 140px">填报单位（盖章）：{{ modelData.reportingUnit }}</div>
			<div>填报时间：{{ modelData.fillingTime }}</div>
		</div>
		<ReportTable
			:column="dataList"
			:isDetail="isDetail"
			:moreNewsModel="moreNewsModel"
			:baseModel="baseModel"
			:model="modelData"
			@change="handleSave"
		>
			<template v-slot:noteS>
				<div class="w-100 tr flex-column"  style="line-height: 30px">
					<div class="p-r-50">
						<p>街（镇）民政部门：（章）</p>
						<p class="tl"  style="padding-right: 1vw;text-align: right;letter-spacing: 10px">年 月 日</p>
					</div>
				</div>
			</template>
			<template v-slot:noteT>
				<div class="w-100 tr flex-column"  style="line-height: 30px">
					<p style="text-align: left;">经现场核查：认定该机构申报床位数<span class="text-underline">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>张，总补贴金额为：<span class="text-underline">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></p>
					<div class="p-r-50">
						<p>第三方机构：（章）</p>
						<p class="tl"  style="text-align: right;letter-spacing: 10px">年 月 日</p>
					</div>
				</div>
			</template>

			<template v-slot:note>
				<div class="w-100 tl flex-column p-r-50" style="line-height:30px">
					<p style="text-align: right;letter-spacing: 4px">区民政部门：（章）</p>
					<p class="tc"  style="text-align: right;letter-spacing: 10px">年 月 日</p>
				</div>
			</template>
		</ReportTable>
		<OrgArchives ref="orgArchivesEl"></OrgArchives>
	</BaseContainer>
</template>

<script setup lang="ts">
import ReportTable from '../ReportTable/index.vue'
import OrgArchives from '../OrgArchives/index.vue'
import apiMod from '@/apiMod'
import { filterEmptyObj } from '@/utils/util'
import { ElMessage, ElMessageBox } from 'element-plus'

const props = defineProps<{
	otherData?: any
	params?: any
	creditCode?: string | number
	editData?: any
	isDetail?: boolean
	caseId?: string | number
	custome?: boolean
	approvalStatus?: string | number
}>()
const emit = defineEmits(['save'])
const pageReload: any = inject('reload')

const model = ref({
	year: '',
	month: '',
})
const baseModel: any = shallowRef({})
const loading = ref(false)
const modelData: any = ref({})
const orgArchivesEl = ref(null)
let moreNewsModel: any = ref({
	organizationInfoPO: {},
})

const tableColumnConfig = (data?: any) => {
	if (!data) data = {}
	return [
		[
			{
				m: '养老机构基本情况',
				bl: 1,
				fs: 16,
				mc: {
					rs: 1,
					cs: 4,
				},
			},
		],
		[
			{ m: '机构名称' },
			{
				m: '',
				prop: 'organizationName',
				setColor: true,
				archives: true,
				archivesText: '档案',
				showLink: (col, model) => {
					orgArchivesEl.value?.getServiceOrgInfo(model.creditCode)
				},
			},
			{ m: '法定代表人', style: { width: '180px' } },
			{
				m: '',
				setColor: true,
				prop: 'legalUserName',
			},
		],
		[
			{ m: '机构地址' },
			{
				m: '',
				setColor: true,
				prop: 'organizationAddress',
			},
			{ m: '邮政编码' },
			{
				m: '',
				setColor: true,
				prop: 'postalCode',
			},
		],
		[
			{ m: '联系人' },
			{
				m: '',
				prop: 'legalUserName',
				setColor: true,
			},
			{ m: '电话' },
			{
				m: '',
				prop: 'legalUserPhone',
				setColor: true,
			},
		],
		[
			{ m: '开户银行' },
			{
				setColor: true,
				m: '',
				prop: 'bankName',
			},
			{
				m: '银行账号',
			},
			{
				setColor: true,
				m: '',
				prop: 'bankAccount',
			},
		],
		[
			{ m: '领取新增补贴床位数' },
			{
				setColor: true,
				m: '',
				prop: 'receiveGeneralBedTotal',
			},
			{
				m: '领取时间',
			},
			{
				setColor: true,
				m: '',
				prop: 'receiveTime',
			},
		],
		[
			{
				m: '申请基本情况',
				bl: 1,
				fs: 16,
				mc: {
					rs: 1,
					cs: 4,
				},
			},
		],
		[
			{ m: '申报床位数' },
			{
				m: '',
				prop: 'applyBedNum',
			},
			{ m: '补贴标准', style: { width: '180px' } },
			{ m: '', prop: 'bedSubsidy' },
		],
		[
			{
				m: '补贴金额：',
				propName: data.applySubsidyAmount,
				mc: {
					rs: 1,
					cs: 2,
				},
			},
			{
				m: '大写：',
				propName: data.applySubsidyAmountCap,
				mc: {
					rs: 1,
					cs: 2,
				},
			},
		],

		[
			{
				m: '街（镇）民政部门意见',
			},
			{
				m: '',
				prop: 'noteS',
				mc: {
					rs: 1,
					cs: 5,
				},
			},
		],

		[
			{
				m: '第三方现场核查意见',
			},
			{
				m: '',
				prop: 'noteT',
				mc: {
					rs: 1,
					cs: 5,
				},
			},
		],

		[
			{
				m: '区民政部门意见',
			},
			{
				m: '',
				prop: 'note',
				mc: {
					rs: 1,
					cs: 5,
				},
			},
		],
	]
}
const dataList = ref(tableColumnConfig())

const getInitData = async () => {
	const params = props.params || {}
	const query = {
		year: '',
		month: '',
		creditCode: props.creditCode,
		projectId: params.subsidyProjectId,
		applySource: !props.approvalStatus ? '-99' : props.approvalStatus,
    uniqueId: props.otherData?.id
	}
	if (params.belongDate) {
		query.year = params.belongDate.replace('年', '-').replace('月', '').split('-')[0]
		query.month = params.belongDate.replace('年', '-').replace('月', '').split('-')[1]
		model.value.year = query.year
		model.value.month = query.month
	} else {
		return
	}

	try {
		const condition = (props.caseId && props.isDetail) || props.custome || props.approvalStatus == -2
		loading.value = true
		let resultData = props.editData
		// if (props.approvalStatus ==undefined && !props.otherData?.allData) {
		if (!condition && !props.otherData?.allData) {
			const uptoData = await apiMod.view_orgBedSubsidyChange(query).then((res) => res.data)
			resultData = uptoData
		}
		baseModel.value = JSON.parse(JSON.stringify(resultData))
		const reportData = filterEmptyObj(resultData)

		let mixData = {
			...props.editData,
			...reportData,
		}
		loading.value = false
		modelData.value = mixData

		if (baseModel.value.isEligible == 0) {
			ElMessageBox.alert(baseModel.value.reason || '当前申请条件不满足！', '提示', {
				showClose: false,
				confirmButtonText: '确定',
				callback: () => {
					pageReload()
				},
			})
			return false
		}

		if (!condition) handleSave()
		dataList.value = tableColumnConfig(reportData)
	} catch (error) {
		loading.value = false
	}
}
onMounted(() => {
	if (props.params) getInitData()
})

const handleSave = () => {
	emit('save', modelData.value, 'bed_report_1006', props.editData)
}

onUnmounted(() => {})
</script>

<style scoped lang="scss">
.report-table-wrapper {
	width: 100%;
	margin: 0 auto;
	padding: 10px 30px;
	top: 0;

	.t-header {
		text-align: center;
		font-size: 18px;
		font-weight: bold;
		padding: 15px 0;

		.text-sm {
			font-weight: 400;
			font-size: 16px;
		}
	}
}

.article-title {
	position: relative;
	z-index: 0;
	width: 63%;
	margin: 0 auto;
}

.text-tip {
	--base-text-bg: rgb(56 176 255 / 13%);
	position: absolute;
	width: 100%;
	height: 100%;
	background: var(--base-text-bg);
	padding: 4px;
	z-index: -1;
	&:hover {
		cursor: pointer;
	}
	.tip-text-new {
		position: absolute;
		right: 0;
		top: -10px;
		color: #fff;
		border-radius: 4px;
		padding: 2px;
		width: 40px;
		background-color: #ed6d05;
	}
	.tip-text-detail {
		background-color: #179fe3;
		position: absolute;
		right: 0;
		top: -10px;
		color: #fff;
		border-radius: 4px;
		padding: 2px;
		width: 40px;
	}
}
</style>
